[% setvar title TITLE %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>counting matches</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Richard Proctor &lt;<a href='mailto:richard@waveney.org'>richard@waveney.org</a>&gt;
  Date: 16 Aug 2000
  Last Modified: 20 Sep 2000
  Mailing List: <a href='mailto:perl6-language-regex@perl.org'>perl6-language-regex@perl.org</a>
  Number: 110
  Version: 6
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Provide a simple way of giving a count of matches of a pattern.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Have you ever wanted to count the number of matches of a patten?  s///g
returns the number of matches it finds.  m//g just returns 1 for matching.
Counts can be made using s//$&amp;/g but this is wastefull, or by putting some
counting loop round a m//g.  But this all seams rather messy.</p>
<p>TomC (and a couple of others) have said that it can also be done as :
$count = () = $string =~ /pattern/g;</p>
<p>However many people do not like this construct, here are a couple of quotes:</p>
<p>jhi: Which I find cute as a demonstration of the Perl's context concept,
but ugly as hell from usability viewpoint.</p>
<p>Bart Lateur: '()=' is not perfect. It is also butt ugly. It is a &quot;dirty hack&quot;.</p>
<p>This construct is also likely to be inefficient as perl will have to
build up a list of all the matches, store them somewhere, count them, then
throw them away.</p>
<p>Therefore I would like a way of counting matches.</p>
<a name='Proposal'></a><h2>Proposal</h2>
<p>m//gt (or m//t see below) would be defined to do the match, and return the
count of matches, this leaves all existing uses consistent and unaffected.
/t is suggested for &quot;counT&quot;, as /c is already taken.</p>
<p>Relationship of m//t and m//g - there are three possibilities, my original:</p>
<p>m//gt, where /t adds counting to a group match (/t without /g would just
return 0 or 1).  However \G loses its meaning.</p>
<p>The Alternative By Uri :</p>
<p>m//t and m//g are mutually exclusive and m//gt should be regarded as an error.</p>
<p>Hugo:</p>
<p>&gt; I like this too. I'd suggest /t should mean a) return a scalar of
&gt; the number of matches and b) don't set any special variables. Then
&gt; /t without /g would return 0 or 1, but be faster since no extra
&gt; information need be captured (except internally for (.)\1 type
&gt; matching - compile time checks could determine if these are needed,
&gt; though (?{..}) and (??{..}) patterns would require disabling of
&gt; that optimisation). /tg would give a scalar count of the total
&gt; number of matches. \G would retain its meaning.</p>
<p>I think Hugo's wording about the relationship makes the best sense, and
this is the suggested way forward.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<p>RFC110 V1 - Original posting to perl6-language</p>
<p>RFC110 V2 - Reposted to perl6-language-regex</p>
<p>RFC110 V3 - Added Uri's alternitive m//t</p>
<p>RFC110 V4 - Added notes about $count = () = $string =~ /pattern/g</p>
<p>RFC110 V5 - Added Hugo's wording about /g and /t relationship, suggested this
is the way forward.</p>
<p>RFC110 V6 - Frozen</p>
<a name='IMPLENTATION'></a><h1>IMPLENTATION</h1>
<p>Hugo:
&gt; Implementation should be fairly straightforward,
&gt; though ensuring that optimisations occurred precisely when they
&gt; are safe would probably involve a few bug-chasing cycles.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>I brought this up on p5p a couple of years ago, but it was lost in the noise...</p>
</div>
